﻿using Abp.Domain.Uow;
using Paas.Core.Const.Enums.Common;
using Basefr.Attributes;
using Basefr.Core.Entities;
using System.Linq;
using System.Transactions;

namespace Paas.Core.Common.Entities
{
    /// <summary>
    /// 序列号
    /// </summary>
    [MainTable(nameof(Serial))]
    public class Serial : EntityBase
    {
        /// <summary>
        /// 对象类型
        /// </summary>
        public EnumObjectType ObjectType { get; set; }

        public int Platform { get; set; }

        public int Device { get; set; }

        public int StockTaking { get; set; }

        public int Lending { get; set; }

        public int Maintenance { get; set; }

        public int Scrapping { get; set; }

        public int Reservation { get; set; }

        public int Calibration { get; set; }


        public string SerialNo
        {
            get
            {
                switch (ObjectType)
                {
                    case EnumObjectType.Platform:
                        return $"PT{Platform:00000}";
                    case EnumObjectType.DeviceSys:
                        return $"XT{Device:00000}";
                    case EnumObjectType.DeviceCard:
                        return $"BK{Device:00000}";
                    case EnumObjectType.DeviceOther:
                        return $"QT{Device:00000}";
                    case EnumObjectType.StockTaking:
                        return $"PD{StockTaking:00000}";
                    case EnumObjectType.Lending:
                        return $"JC{Lending:00000}";
                    case EnumObjectType.Maintenance:
                        return $"WX{Maintenance:00000}";
                    case EnumObjectType.Scrapping:
                        return $"BF{Scrapping:00000}";
                    case EnumObjectType.Reservation:
                        return $"YY{Reservation:00000}";
                    case EnumObjectType.Calibration:
                        return $"JZ{Calibration:00000}";
                    case EnumObjectType.DeviceHard:
                        return $"YJ{Device:00000}";
                }

                return "";
            }
        }

        [UnitOfWork(IsolationLevel.RepeatableRead)]
        public static Serial Create(IMainRepository repo, EnumObjectType objectType)
        {
            var serial = new Serial { ObjectType = objectType };

            switch (objectType)
            {
                case EnumObjectType.Platform:
                    serial.Platform = repo.GetAll<Serial>().Max(p => p.Platform) + 1;
                    break;
                case EnumObjectType.DeviceSys:
                case EnumObjectType.DeviceCard:
                case EnumObjectType.DeviceOther:
                    serial.Device = repo.GetAll<Serial>().Max(p => p.Device) + 1;
                    break;
                case EnumObjectType.StockTaking:
                    serial.StockTaking = repo.GetAll<Serial>().Max(p => p.StockTaking) + 1;
                    break;
                case EnumObjectType.Lending:
                    serial.Lending = repo.GetAll<Serial>().Max(p => p.Lending) + 1;
                    break;
                case EnumObjectType.Maintenance:
                    serial.Maintenance = repo.GetAll<Serial>().Max(p => p.Maintenance) + 1;
                    break;
                case EnumObjectType.Scrapping:
                    serial.Scrapping = repo.GetAll<Serial>().Max(p => p.Scrapping) + 1;
                    break;
                case EnumObjectType.Reservation:
                    serial.Reservation = repo.GetAll<Serial>().Max(p => p.Reservation) + 1;
                    break;
                case EnumObjectType.Calibration:
                    serial.Calibration = repo.GetAll<Serial>().Max(p => p.Calibration) + 1;
                    break;
                case EnumObjectType.DeviceHard:
                    serial.Device = repo.GetAll<Serial>().Max(p => p.Device) + 1;
                    break;
            }

            repo.InsertAndGetId(serial);

            return serial;
        }

    }
}

